package com.handinfo.android.uicontrols.controls;


import android.graphics.Bitmap;

import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.utils.Tools;
/**
 * 百分比条控件
 * @author handinfo
 */
public class DWPercentBar extends DWControl
{
	private long m_total_lenght = 10;//总长度
	private long m_current_lenght = 0;//当前长度
	private Bitmap img_foreground = null;//前景图片
	private Bitmap img_background = null;//背景图片
	private int m_fimg_w = 0;//前景图片宽度
	private int m_fimg_h = 0;//前景图片高度
	private int m_bimg_w = 0;//背景图片宽度
	private int m_bimg_h = 0;//背景图片高度
	private int m_foreground_show_w = 0;//前景显示宽度
	private int m_foreground_show_x = 0;//前景显示x
	private int m_foreground_show_y = 0;//前景显示y
	private boolean m_is_turn = false;//是否翻转百分比条
	/**
	 * 创建一个百分比条
	 * @param lenght 显示宽度
	 * @param foreground 前景图片
	 * @param background 背景图片
	 */
	public DWPercentBar(int lenght, Bitmap foreground, Bitmap background) {
		this(lenght, foreground, background, 0, 0);
	}
	/**
	 * 创建一个百分比条
	 * @param lenght 显示宽度
	 * @param foreground 前景图片
	 * @param background 背景图片
	 * @param x
	 * @param y
	 */
	public DWPercentBar(int lenght, Bitmap foreground, Bitmap background, int x, int y) {
		if (foreground == null) {
			Tools.debugPrintln("DWPercentBar foreground is null!");
		}
		if (background == null) {
			Tools.debugPrintln("DWPercentBar background is null!");
		}
		img_foreground = foreground;
		m_fimg_w = img_foreground.getWidth();
		m_fimg_h = img_foreground.getHeight();
		
		img_background = background;
		m_bimg_w = img_background.getWidth();
		m_bimg_h = img_background.getHeight();
		
		m_foreground_show_w = lenght - (m_bimg_h - m_fimg_h);
		m_foreground_show_x = (m_bimg_h - m_fimg_h) / 2;
		m_foreground_show_y = m_foreground_show_x;
		
		if (m_foreground_show_w < 0) {
			Tools.debugPrintln("DWPercentBar freground height > background height!");
		}
		
		setPoint(x, y);
		setShowWideHigh(lenght, img_background.getHeight());
	}
	
	/**
	 * 设置数值
	 * @param current 当前数
	 * @param total 总数
	 */
	public void setPercent(long current, long total) {
		if (current > total) {
			Tools.debugPrintln("DWPercentBar current > total!");
		}
		m_current_lenght = current;
		m_total_lenght = total;
	}
	
	/**
	 * 设置反向显示百分比条
	 * @param turn
	 */
	public void setTurn(boolean turn) {
		m_is_turn = turn;
	}

	public void render(DWGraphics g)
	{
		if (!m_is_show) {
			return;
		}
		g.setClip(m_show_x, m_show_y, m_show_w, m_show_h);
		for (int i = 0; i < m_show_w; i += m_bimg_w) {
			g.drawBitmap(img_background, m_show_x + i, m_show_y, Tools.LEFT_TOP);
		}
		float percent = m_current_lenght * 100 / m_total_lenght * m_foreground_show_w / 100;
		if (m_is_turn) {
			int ox = m_foreground_show_w - (int)percent;
			g.setClip(m_show_x + m_foreground_show_x + ox, m_show_y, (int)percent, m_show_h);
			for (int i = 0; i < m_foreground_show_w; i += m_fimg_w) {
				g.drawBitmap(img_foreground,
						m_show_x + m_foreground_show_x + ox + i,
						m_show_y + m_foreground_show_y, Tools.LEFT_TOP);
			}
		} else {
			g.setClip(m_show_x + m_foreground_show_x, m_show_y, (int)percent, m_show_h);
			for (int i = 0; i < m_foreground_show_w; i += m_fimg_w) {
				g.drawBitmap(img_foreground, m_show_x + m_foreground_show_x + i, m_show_y + m_foreground_show_y, Tools.LEFT_TOP);
			}
		}
	}
	
	public void renderLimits(DWGraphics g, int x, int y, int w, int h, int offsetx, int offsety)
	{
		if (!m_is_show) {
			return;
		}
		setSubClip(g, x, y, w, h, offsetx, offsety);
		for (int i = 0; i < m_show_w; i += m_bimg_w) {
			g.drawBitmap(img_background, x + m_show_x + i, y + m_show_y, Tools.LEFT_TOP);
		}
		int percent = (int)(m_current_lenght * 100 / m_total_lenght * m_foreground_show_w / 100);
		for (int i = 0, j = 0; i < percent; i += m_fimg_w) {
			j = 0;
			if (i > percent) {
				j = i - percent;
			}
			g.drawBitmap(img_foreground, x + m_show_x + m_foreground_show_x + i - j, y + m_show_y + m_foreground_show_y, Tools.LEFT_TOP);
		}
	}

	public void logic()
	{
		
	}
	
	public void refreshControl()
	{
		
	}

	public boolean doClick(float pointX, float pointY)
	{
		return false;
	}

}
